﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Marbles.Core.Connectors;

namespace Marbles.Core.BBS
{
    /// <summary>
    /// Forum Category Database Object
    /// </summary>
    public class ForumCategory
    {
        /// <summary>
        /// SiteId of Forum Category
        /// </summary>
        public string SiteId { get { return _siteid; } }
        /// <summary>
        /// Category Name
        /// </summary>
        public string Category { get { return _category; } }
        /// <summary>
        /// Title
        /// </summary>
        public string Title { get { return _title; } set { _title = value.Trim(); } }

        private string _siteid = string.Empty;
        private string _category = string.Empty;
        private string _title = string.Empty;

        public ForumCategory()
        {
            _siteid = string.Empty;
            _title = string.Empty;
            _category = string.Empty;
        }

        public ForumCategory(string siteid, string category)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT siteid, category, title FROM forumcategory WHERE siteid = @siteid AND category = @category");
            cmd.AddParameter("@siteid", siteid);
            cmd.AddParameter("@category", category);

            DataTable dt = dbc.FetchDataTable(cmd);
            if (dt.Rows.Count > 0)
            {
                _title = dt.Rows[0]["title"].ToString();
                _siteid = dt.Rows[0]["siteid"].ToString();
                _category = dt.Rows[0]["category"].ToString();
            }
        }

        internal ForumCategory(DataRow row)
        {
            _siteid = row["siteid"].ToString();
            _category = row["category"].ToString();
            _title = row["title"].ToString();
        }

        /// <summary>
        /// Creates a new instance of ForumCategory
        /// </summary>
        /// <param name="SiteId">SiteId of Associated Site</param>
        /// <param name="Category">Category Name</param>
        /// <returns>ForumCategory</returns>
        public static ForumCategory CreateNew(string SiteId, string Category)
        {
            ForumCategory cat = new ForumCategory();
            cat._siteid = SiteId;
            cat._category = Category.Trim().ToLower();
            return cat;
        }

        public void Delete()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("BEGIN TRANSACTION;DELETE FROM forumcategory WHERE siteid = @siteid AND category = @category;UPDATE forumheader SET category = '' WHERE siteid = @siteid AND category = @category; COMMIT TRANSACTION;");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@category", _category);
            dbc.ExecuteCommand(cmd);
        }

        public void Insert()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("INSERT INTO forumcategory (siteid, category, title) VALUES (@siteid, @category, @title)");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@category", _category);
            cmd.AddParameter("@title", _title);
            dbc.ExecuteCommand(cmd);
        }

        public void Update()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("UPDATE forumcategory SET title = @title WHERE siteid = @siteid AND category = @category");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@category", _category);
            cmd.AddParameter("@title", _title);
            dbc.ExecuteCommand(cmd);
        }
    }
}
